<template>
  <div class="markdown-content" v-html="renderedContent"></div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { marked } from 'marked'
import hljs from 'highlight.js'
import 'highlight.js/styles/github.css'

// Configure marked to use highlight.js for code syntax highlighting
marked.setOptions({
  highlight: function(code, lang) {
    if (lang && hljs.getLanguage(lang)) {
      try {
        return hljs.highlight(code, { language: lang }).value
      } catch (e) {
        console.error(e)
      }
    }
    return hljs.highlightAuto(code).value
  },
  breaks: true,
  gfm: true
})

const props = defineProps<{
  content: string
}>()

const renderedContent = computed(() => {
  try {
    return marked(props.content || '')
  } catch (e) {
    console.error('Markdown rendering error:', e)
    return ''
  }
})
</script>

<style scoped>
.markdown-content {
  line-height: 1.6;
  color: var(--el-text-color-primary);
}

.markdown-content :deep(h1) {
  font-size: 2em;
  margin: 0.67em 0;
  border-bottom: 1px solid var(--el-border-color-light);
  padding-bottom: 0.3em;
}

.markdown-content :deep(h2) {
  font-size: 1.5em;
  margin: 0.83em 0;
  border-bottom: 1px solid var(--el-border-color-light);
  padding-bottom: 0.3em;
}

.markdown-content :deep(h3) {
  font-size: 1.17em;
  margin: 1em 0;
}

.markdown-content :deep(p) {
  margin: 1em 0;
}

.markdown-content :deep(ul), 
.markdown-content :deep(ol) {
  padding-left: 2em;
  margin: 1em 0;
}

.markdown-content :deep(code) {
  background-color: var(--el-fill-color-light);
  padding: 0.2em 0.4em;
  border-radius: 3px;
  font-family: 'Fira Code', monospace;
  font-size: 0.9em;
}

.markdown-content :deep(pre) {
  background-color: var(--el-fill-color-light);
  padding: 1em;
  border-radius: 6px;
  overflow: auto;
  margin: 1em 0;
}

.markdown-content :deep(pre code) {
  background-color: transparent;
  padding: 0;
  border-radius: 0;
  font-size: 0.9em;
}

.markdown-content :deep(blockquote) {
  margin: 1em 0;
  padding: 0 1em;
  color: var(--el-text-color-regular);
  border-left: 0.25em solid var(--el-border-color);
}

.markdown-content :deep(table) {
  border-collapse: collapse;
  margin: 1em 0;
  width: 100%;
}

.markdown-content :deep(th),
.markdown-content :deep(td) {
  border: 1px solid var(--el-border-color);
  padding: 6px 13px;
}

.markdown-content :deep(th) {
  background-color: var(--el-fill-color-light);
}

.markdown-content :deep(img) {
  max-width: 100%;
  height: auto;
}

.markdown-content :deep(hr) {
  height: 1px;
  border: none;
  background-color: var(--el-border-color);
  margin: 1em 0;
}
</style>
